﻿using PHMEE.Constants;
using PHMEE.Entities.BaseInfo.Points;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;

namespace PHMEE
{
    public abstract class Point : FullAuditedAggregateRoot<Guid>
    {
        protected Point()
        {

        }
        public Point(Guid id, string name, string nameen) : base(id)
        {
            SetName(name);
            SetNameEn(nameen);
        }

        /// <summary>
        /// 测点名称
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 英文名
        /// </summary>
        public string NameEn { get; private set; }

        /// <summary>
        /// 标签
        /// </summary>
        public BaseInfoTagEnum? Tag { get; set; }

        /// <summary>
        /// 测点分类
        /// </summary>
        public PointCategoryEnum Category { get; set; }

        /// <summary>
        /// 隶属对象Id
        /// </summary>
        public Guid OwnerId { get; set; }

        /// <summary>
        /// 隶属对象Type
        /// </summary>
        public string OwnerType { get; set; }

        /// <summary>
        /// 测点类型
        /// </summary>
        public PointTypeEnum Type { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }

        public string Language => CultureInfo.CurrentCulture.TwoLetterISOLanguageName;

        /// <summary>
        /// 多语言展示Name
        /// </summary>
        public string DisplayName => GetLocalizedName();


        public Dictionary<string, object> DerivedPropertiesJson { get; set; } = new Dictionary<string, object>();

        public abstract void SerializeProperties(Dictionary<string, object> derivedPropertiesJson);
        public abstract void DeserializeProperties();

        public Point SetName(string name)
        {
            Name = Check.NotNullOrEmpty(name, nameof(name), PointConst.MaxNameLength);
            return this;
        }

        public Point SetNameEn(string nameen)
        {
            NameEn = Check.NotNullOrEmpty(nameen, nameof(nameen), PointConst.MaxNameLength);
            return this;
        }

        public static List<PointTypeEnum> GetPointTypeOptions(PointCategoryEnum category)
        {
            var dict = GetPointCategoryToTypeOptions();

            if (dict.TryGetValue(category, out var pointTypeList))
            {
                return pointTypeList;
            }
            return new List<PointTypeEnum>(); // 返回空列表，如果没有匹配项
        }

        public static Dictionary<PointCategoryEnum, List<PointTypeEnum>> GetPointCategoryToTypeOptions()
        {
            var dict = new Dictionary<PointCategoryEnum, List<PointTypeEnum>>
            {
                { PointCategoryEnum.Vibrate, new List<PointTypeEnum> { PointTypeEnum.Acceleration,PointTypeEnum.Speed,PointTypeEnum.Displacement } },
                { PointCategoryEnum.Technology, new List<PointTypeEnum> { PointTypeEnum.Speed, PointTypeEnum.RevSpeed, PointTypeEnum.Temperature,PointTypeEnum.Pressure,PointTypeEnum.Current,PointTypeEnum.Voltage,PointTypeEnum.Flow,PointTypeEnum.Level,PointTypeEnum.Longitude,PointTypeEnum.Latitude,PointTypeEnum.Draft,PointTypeEnum.Power,PointTypeEnum.Load,PointTypeEnum.FuelConsumption,PointTypeEnum.Torque, PointTypeEnum.MainEngineStatus, PointTypeEnum.FuelType, PointTypeEnum.Angle, PointTypeEnum.Viscosity, PointTypeEnum.PowerFactor, PointTypeEnum.Frequency } }
            };

            return dict;
        }

        private string GetLocalizedName()
        {
            string localizedName = Language switch
            {
                PhmeeConst.LanguageEn => NameEn,
                _ => Name
            };

            return localizedName;
        }

    }
}
